package com.dadibadi.core.auth.controller;


import com.dadibadi.core.auth.qo.SysRoleEditQO;
import com.dadibadi.core.dao.entity.SysRoleTab;
import com.dadibadi.core.dao.mapper.SysRoleMapper;
import com.venusoft.developer.common.result.CommonResult;
import com.venusoft.developer.common.result.ICommonResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * TODO
 *
 * @author : lkfs@163.com
 * @date : 2025-08-26 16:45
 */

@RestController
@RequestMapping("/api")
@Slf4j
@Tag(name = "api/角色管理", description = "")
public class AdminAccountRoleSaveController {
    @Resource
    private SysRoleMapper sysRoleMapper;

    @PostMapping("/admin-sys-role-save")
    @Operation(summary = "保存", description = "")
    public ICommonResult exec(
            @RequestBody @Parameter(required = true) @Validated SysRoleEditQO qo,
            @RequestHeader(required = false) String platform,
            @RequestHeader(required = false) String version,
            @RequestHeader(required = false, name = "Accept-Language") String language,
            HttpServletRequest request) {

        SysRoleTab sysRoleTab;
        sysRoleTab=sysRoleMapper.selectById(qo.getUserId());
        if (sysRoleTab==null) {
            sysRoleTab = new SysRoleTab();
            BeanUtils.copyProperties(qo, sysRoleTab);
            sysRoleTab.setRoleType("USER");
            sysRoleMapper.insert(sysRoleTab);
        } else {
            sysRoleTab = sysRoleMapper.selectById(qo.getUserId());
            BeanUtils.copyProperties(qo, sysRoleTab);
            sysRoleMapper.updateById(sysRoleTab);
        }

        return new CommonResult<>(sysRoleTab.getUserId());

    }
}
